unit umethods;

{$mode objfpc}

interface

uses
  Classes, strutils;

function write_bat_nbtscan_win(net, workdir : String): Boolean;
function write_bat_netview(domain_str, workdir : String): Boolean;
function write_eicar_file(eicarfile: String): Boolean;
procedure parse_nmblookup_unix(ipdata,opdata : TStringList);
procedure parse_nbtscan_unix(ipdata,opdata : TStringList);
procedure parse_nbtscan_win32(ipdata,opdata : TStringList);
procedure parse_netview_win32(ipdata,opdata : TStringList);

const
  patteicar = 'X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*';

implementation

//Write bat file for nbtscan under MS Windows. Net param es subnet for scan
function write_bat_nbtscan_win(net, workdir : String): Boolean;
 var
  content_bat : TStringList;
  writeok : Boolean;
  buf : String;
begin
 writeok:=false;
 try
  buf:='nbtscan.exe '+net+' >nbtscan.dat';
  content_bat := TStringList.Create;
  content_bat.Add(buf);
  content_bat.SaveToFile(workdir+'nbtscan.bat');
  writeok:=True
 finally
  content_bat.Free;
 end;
 Result:=writeok;
end;

//Write bat file for netview command. Domain_str is Netbios Domain Name for explorer
function write_bat_netview(domain_str, workdir: String): Boolean;
var
   content_bat : TStringList;
   writeok : Boolean;
   buf,domain : String;
begin
 domain:='';
 writeok:=false;
 if Length(domain_str) >1 then
   domain:=' /DOMAIN:'+domain_str+' >netview.dat';
 try
  buf:='net view'+domain;
  content_bat := TStringList.Create;
  content_bat.Add(buf);
  content_bat.SaveToFile(workdir+'netview.bat');
  writeok:=True
 finally
  content_bat.Free;
 end;
 Result:=writeok;
end;

//Write EICAR file.
function write_eicar_file(eicarfile : String): Boolean;
var
 filestream : TStringList;
 vresult    : Boolean;
begin
 vresult:=False;
 try
  filestream := TStringList.Create;
  filestream.Add(patteicar);
  filestream.SaveToFile(eicarfile);
  vresult:=True;
 finally
  filestream.Free;
 end;
 Result := vresult;
end;

procedure parse_nmblookup_unix(ipdata, opdata: TStringList);
var
  i        : integer;
  hostname : string;
begin
 ipdata.NameValueSeparator:=',';
  for i :=1 to ipdata.Count-1 do
   begin
     hostname:=DelSpace(ipdata.Names[i]);
     if Length(hostname) >= 2 then;
       opdata.Add(hostname);
   end;
 opdata.Sort;
end;

procedure parse_nbtscan_unix(ipdata, opdata: TStringList);
var
  i        : integer;
  hostname : string;
begin
 ipdata.NameValueSeparator:=',';
  for i :=0 to ipdata.Count-1 do
   begin
     hostname:=DelSpace(ipdata.ValueFromIndex[i]);
     hostname:=Copy2Symb(hostname,',');
     if Length(hostname) >= 2 then;
       opdata.Add(hostname);
   end;
  opdata.Sort;
end;

procedure parse_nbtscan_win32(ipdata, opdata: TStringList);
var
  i,pos_slash        : integer;
  hostname           : string;
begin
  for i:=0 to ipdata.Count-1 do
    begin
      if Pos('\',ipdata.Strings[i]) >0 then
         begin
          pos_slash:=Pos('\',ipdata.Strings[i]);
          hostname:=copy(ipdata.Strings[i],pos_slash+1,Length(ipdata.Strings[i]));
          hostname:=Copy2Space(hostname);
          if Length(hostname) >= 2 then;
             opdata.Add(hostname);
         end;
    end;
  opdata.Sort;
end;

procedure parse_netview_win32(ipdata, opdata: TStringList);
var
  i        : integer;
  hostname : string;
begin
 for i :=0 to ipdata.Count-1 do
   if copy(ipdata.Strings[i],1,1) = '\' then
       begin
        hostname:=copy(ipdata.Strings[i],3,Length(ipdata.Strings[i]));
        hostname:=Copy2Space(hostname);
        if Length(hostname) >= 2 then;
           opdata.Add(hostname);
       end;
 opdata.Sort;
end;

end.

